<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>关联采购单</title>
<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/jquery/jquery-1.3.2.min.js"	type="text/javascript"></script>
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.form.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.validate.method.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>
<script src="${Plugins}/datepicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/json2.js"></script>

<style type="text/css">
    body{ font-size:12px;}
    .l-table-edit {}
    .l-table-edit-td{ padding:4px;}
    .l-button-submit,.l-button-test{width:80px; float:left; margin-left:10px; padding-bottom:2px;}
    .l-verify-tip{ left:230px; top:120px;}
    .navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
	.p-tab .s-lab{font-size:12px;font-family:"微软雅黑",​"宋体",​Arial,​sans-serif;}
	input.l-textarea{width:170px;height:20px;font-size:12px;}
</style>
</head>
<body style="padding:10px">
<div class="navbar">
		<span>您当前的位置：</span>
		出入库管理  &gt;出库管理 &gt;关联采购单
</div>
<form name="form1" method="post" action="addStockTransfer.do" id="form1">
   <table  cellpadding="0" cellspacing="0" class="l-table-edit p-tab">
    <tr>
        <td align="right" class="l-table-edit-td s-lab"></td>
        <td align="left" width="100%" class="l-table-edit-td "></td>
        <td align="left"></td>
    </tr>
     <tr>
     	<td>
	     		调出仓库：
	   </td>
     	<td id = "whTd">
	     	 <select id="transferOutWhNo"  name="transferOutWhNo">
	            <option value="">请选择</option>
	            <c:forEach var="temp" items="${whareHouse}">
	            	<option value="${temp.whNo}">${temp.whName}</option>
	            </c:forEach>
	        </select>
	    </td>
	 </tr>
	 <tr style="height: 9px">
     		<td colspan="4"></td>
    	</tr>
     <tr>
	    <td>
	     		调入仓库：
	    </td>
     	<td id = "whTd2">
		     	 <select id="transferInWhNo"  name="transferInWhNo">
		            <option value="">请选择</option>
		            <c:forEach var="temp" items="${whareHouse}">
		            	<option value="${temp.whNo}">${temp.whName}</option>
		            </c:forEach>
		        </select>
	    </td>
	</tr>
	<tr style="height: 9px">
     		<td colspan="4"></td>
    	</tr>
     <tr>
        <td align="right" class="l-table-edit-td s-lab">
        	<input type="button" value="选择商品" id="select_product" class="l-button l-button-test" />
        	<input type="hidden" id="productlist" name="productlist" />
        </td>
        <td align="right"  class="l-table-edit-td s-lab">
            <input type="button" value="保存" id="add_pur" class="l-button l-button-test" onclick="addStockTransfer()"/> 
        </td>
     </tr>
     <tr>
        <td colspan="3" align="left" class="l-table-edit-td s-lab">
        <div id="maingrid" style="margin:0; padding:0"></div>
		</td>
     </tr>
     <tr>
        <td colspan="3" align="left" class="l-table-edit-td s-lab">
		</td>
     </tr>
</table>
<div style="display:none;"></div>
</form>
</body>
<script>
	var basePath ="${BasePath}";
	var addUrl = basePath + '/warehouse/stockTransferController/addStockTransfer.do';
	$(document).ready(function(){
	
		$("#select_product").click(function(){
			list = new Array();
		   	var m =$.ligerDialog.open({
		   				title:'选择库存商品',
		   				url: basePath + '/warehouse/stockTransferController/toQueruyStockProduct.do?whNo='+ $('#transferOutWhNo').val(),
		   				height:500,width:1000,
		   				buttons: [{ text: '确定', onclick: function (item, dialog) { 
											dialog.frame.valProd();
		   									if(!dialog.frame.isOut) return false;
		   									addProduct(); 
		   									dialog.close(); 
		   								} 
		   						  },
		   				          { text: '取消', onclick: function (item, dialog) { dialog.close(); list = new Array();} } ],
		   				isResize: false, 
		   				isDrag: false 
		   		});
		   	m.show(); 
		});
		
		addProduct();
	});
	var list = new Array();
	var commoditylist = new Array();
	var g = null;
	function addProduct(){
		commoditylist = commoditylist.concat(list);
		commodityData = {"Rows": commoditylist, "Total": commoditylist.length};
		grid = $("#maingrid").ligerGrid({
	        columns: [
	      //{ display: 'id', name: 'stockPurId' },
	        { display: '货品编号', name: 'prodNo' },
	        { display: '商品编号', name: 'commoNo' },
	        { display: '商品名称', name: 'commoName' },
			{ display: '供应商', name: 'supName' },
	      	{ display: '规格', name: 'specName'},
	        { display: '库存可调数', name: 'maxQualifiedQuantity'},
	       // { display: '库存次品可调数', name: 'maxDefectiveQuantity'},
	        { display: '需调拨数', name: 'qualifiedQuantity',editor:{type:'int'},
	             render:function(rowdata,rowindex,value){
					return "<font style='color:blue'>"+value+"</font>";
	     		}
	        },
	       /*  { display: '需调拨次品数', name: 'defectiveQuantity',editor:{type:'int'},
	             render:function(rowdata,rowindex,value){
					return value;
	     		}
	        }, */
	        { display: '操作', isSort: false, render: function (rowdata, rowindex, value){
	           	var  text = "<input type='button' value='删除' onclick='removeThis("+rowindex+");' />";
	            return text;
	        	}
	        }
	        ], 
	        width: '100%',
	        height: '330',
	        usePager: false,enabledEdit: true,
	        data: $.extend(true,{},commodityData)
	    });
		list = new Array();
		g= $("#maingrid").ligerGrid({
			grid: grid,
			pageSize:30,
			width: '100%',
			height: '92%',
			dataType: 'server', 
			dataAction: 'server',
			pagesizeParmName: 'pageSize',
			enabledEdit: true,
			onBeforeSubmitEdit: onBeforeSubmitEdit
		});
	}

	function getSubmitData(){
		var submitData = [];
		var datas = g.getData(); 
		for(var i = 0;i< datas.length;i++){
			var data = {};
			data.transferOutWhNo = $('#transferOutWhNo').val();
			data.transferInWhNo = $('#transferInWhNo').val();
			submitData[i] = data;
		}
		return submitData;
	 }
	 
	 function addStockTransfer(){
		 if(vailGridData()){
			 $.ajax({
					type : 'post',
					dataType : 'text',
					data : {
						transferOutWhNo : $('#transferOutWhNo').val(),
						transferOutWhName : $("#transferOutWhNo").find("option:selected").text(),
						transferInWhNo : $('#transferInWhNo').val(),
						transferInWhName : $("#transferInWhNo").find("option:selected").text(),
						products : JSON.stringify(g.getData())
					},
					url : addUrl,
					success : function(data) {
						if (data == 'fail') {
							window.parent.parent.f_tip('新增失败');
						} else {
							window.parent.parent.f_tip('新增成功');
							 operateSuccess();
						}
					}
				});
		 }
	 }
	 function vailGridData(){
		var transferOutWhNo = $('#transferOutWhNo').val();
		if(transferOutWhNo == null || transferOutWhNo == ""){
			window.parent.parent.f_tip('请选择调拨出库的仓库');
			return false;
		} 
		var transferInWhNo = $('#transferInWhNo').val();
		if(transferInWhNo == null || transferInWhNo == ""){
			window.parent.parent.f_tip('请选择调拨入库的仓库');
			return false;
		}
		
		if(transferOutWhNo == transferInWhNo){
			window.parent.parent.f_tip('调拨出库和入库不能为同一个仓库');
			return false;
		}
		if(g.getData().length == 0){
			window.parent.parent.f_tip('请选择调拨的商品');
			return false;
		}
		return true;
	}
	 
	 var parentTabId = ${tabId}; 
	 var tabId = window.top.tab.getSelectedTabItemID();
 	function operateSuccess(){
		window.top.tab.selectTabItem(parentTabId);
		window.top.tab.reload(parentTabId);
		window.top.tab.removeTabItem(tabId);
	}
	function onBeforeSubmitEdit(e){ 
		var record = e.record;
		var column = e.column;
		var editValue = e.value;
		if(column.name == "qualifiedQuantity"){
			var maxQuantity = record.maxQualifiedQuantity;
			if(editValue < 1){
				e.value = 1;
				window.parent.parent.f_tip('该商品调拨数量至少为1件');
			}else if(editValue > maxQuantity){
				e.value = maxQuantity;
				window.parent.parent.f_tip('该商品调拨数量不能大于' + maxQuantity+'件');
			}
		}
	}
	 
	function removeThis(o) {
		commoditylist.splice(o,1);
		addProduct();
	}

	//from提交
	$(function ()
			{
			    $.metadata.setType("attr", "validate");
			    var v = $("form").validate({
			        errorPlacement: function (lable, element)
			        {
			        	lable.ligerHideTip();
			            if (element.hasClass("l-textarea"))
			            {
			                element.ligerTip({ content: lable.html(), target: element[0] }); 
			            }
			            else if (element.hasClass("l-text-field"))
			            {
			                element.parent().ligerTip({ content: lable.html(), target: element[0] });
			            }
			            else
			            {
			                lable.appendTo(element.parents("td:first").next("td"));
			            }
			            
			        },
			        submitHandler:function()
			        {
			        	$("form .l-text,.l-textarea").ligerHideTip();
			            var options = {
			            		type: 'POST',
			            		beforeSend: function(){
			            			$("#form1").find(":submit,:button").attr("disabled", true);
			            		},
			            		success: function(data){
			            			if(data=="success") {
			            				window.parent.f_tip('新增成功');
			            				close_liger_tab();
			            			} else {
			            				window.parent.f_tip('新增失败');
			            			}
			            		}
			            	}, $form1 = $("#form1");
			           $form1.ajaxSubmit(options);
			           return false;
			        }
			    });
	}); 
</script>
</html>